//
//  GoodsInforViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/17.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GoodsInforViewModel.h"
#import "GoodsPictureHeaderView.h"
#import "GoodsBottomView.h"
#import "StellNameTableViewCell.h"
#import "TheStoreTableViewCell.h"
#import "StoreListTitleTableViewCell.h"
#import "StoreListInforTableViewCell.h"
#import "StorePayInforTableViewCell.h"
#import "FreeGoodsInforBottomView.h"
#import "GoodsInforSysView.h"
#import "GoodsInformationModel.h"


@interface GoodsInforViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    GoodsInformationModel * inforModel;
    UIView * self_View;
    UITableView * goodsTableView;
}
@end

@implementation GoodsInforViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self_View = superView;
        CGFloat navHight;
        if (iPhoneX) {
            navHight = 88;
        }else{
            navHight = 64;
        }
        tableView.y = tableView.y - navHight;
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(60) + navHight;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.tableHeaderView = [self createHeaderView];
        [superView addSubview:tableView];
        goodsTableView = tableView;
    }
    return self;
}
-(UIView *)createHeaderView{
    GoodsPictureHeaderView * headerView = [[GoodsPictureHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(250))];
    return headerView;
}

-(void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    //
    if (self.accountType == AccountType_FreeManagers) {
        if (self.isSelect) {
            goodsTableView.height = goodsTableView.height + kHeightProportion_In_iPhoneTriploid(16);
            GLGWButton * bottomBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(goodsTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) hTitle:@"选择它" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor whiteColor] theCornerRadius:0 backgroundColr:kColor_Rule_Yellow inSuperView:self_View];
            [bottomBut addTarget:self action:@selector(selectGoods) forControlEvents:UIControlEventTouchUpInside];
        }else{
            FreeGoodsInforBottomView * freeBottomView = [[FreeGoodsInforBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(goodsTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(60)) inSuperView:self_View];
            freeBottomView.Block_FreeGoodsInfor = ^(GLGWButton * _Nonnull f_but) {
                switch (f_but.tag) {
                    case 300:{//同步
                        GoodsInforSysView * syncGoodsInforView = [[GoodsInforSysView alloc] initWithGoodsInformationSync];
                        syncGoodsInforView.Block_SysncAction = ^{
                            
                        };
                    }
                        break;
                    case 301:{//店铺
                        HightQualityStoreModel_Infos * model = [HightQualityStoreModel_Infos new];
                        model.store_id = self->inforModel.data.shopId;
                        model.shopName = self->inforModel.data.shopName;
                        [self.delegate pushGoodsStore:model];
                    }
                        break;
                    case 302:{//联系卖家
                        
                    }
                        break;
                        
                    default:
                        break;
                }
            };
            
        }
        
        
    }else{
        GoodsBottomView * bottomView = [[GoodsBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(goodsTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(60)) inSuperView:self_View];
        bottomView.Block_BottomAction = ^(GLGWButton * _Nullable a_but) {
            switch (a_but.tag) {
                case 170:{//加入购物车
                    
                }
                    break;
                case 171:{//立即购买
                    [self.delegate buyNowGoods];
                }
                    break;
                case 172:{//店铺
                    HightQualityStoreModel_Infos * model = [HightQualityStoreModel_Infos new];
                    model.store_id = self->inforModel.data.shopId;
                    model.shopName = self->inforModel.data.shopName;
                    [self.delegate pushGoodsStore:model];
                }
                    break;
                case 173:{//联系卖家
                    
                }
                    break;
                case 174:{//购车
                    [self.delegate pushShopCar];
                }
                    break;
                    
                default:
                    break;
            }
        };
    }
}

-(void)selectGoods{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectGoodsAction)]) {
        [self.delegate selectGoodsAction];
    }
}

-(void)setG_id:(NSInteger)g_id{
    _g_id = g_id;
    [self requestDataGoodsInfor];
}

#pragma mark -- 数据请求
-(void)requestDataGoodsInfor{
    NSDictionary * dic = @{@"id":@(_g_id),@"roleType":@(self.accountType +1)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kShopProductinfo] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        self->inforModel = [GoodsInformationModel yy_modelWithJSON:response];
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return 8;
    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    switch (indexPath.section) {
        case 0:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(123);
        }
            break;
        case 1:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(108);
        }
            break;
        case 2:{
            if (indexPath.row == 0) {
                cellHeight = kHeightProportion_In_iPhoneTriploid(50);
            }else{
                cellHeight = kHeightProportion_In_iPhoneTriploid(44);
            }
        }
            break;
        case 3:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(134);
        }
            break;
            
        default:
            cellHeight = 0.01;
            break;
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            StellNameTableViewCell * cell_stell = [StellNameTableViewCell cellWithTableView:tableView];
            cell_stell.Block_CalatorAction = ^{
                [self.delegate pushCalculator];
            };
            cell = cell_stell;
        }
            break;
        case 1:{
            TheStoreTableViewCell * cell_store = [TheStoreTableViewCell cellWithTableView:tableView];
            cell_store.Block_ConverStore = ^(GLGWButton * _Nonnull s_but) {
                if (s_but.tag == 311) {
                    HightQualityStoreModel_Infos * model = [HightQualityStoreModel_Infos new];
                    model.store_id = self->inforModel.data.shopId;
                    model.shopName = self->inforModel.data.shopName;
                    [self.delegate pushGoodsStore:model];
                }
            };
            cell = cell_store;
        }
            break;
        case 2:{
            if (indexPath.row == 0) {
                StoreListTitleTableViewCell * cell_titLe = [StoreListTitleTableViewCell cellWithTableView:tableView];
                cell = cell_titLe;
            }else{
                StoreListInforTableViewCell * cell_infor = [StoreListInforTableViewCell cellWithTableView:tableView];
                [cell_infor setSepartionLineWith:lineModel_Half];
                cell = cell_infor;
            }
        }
            break;
        case 3:{
            StorePayInforTableViewCell * cell_pay = [StorePayInforTableViewCell cellWithTableView:tableView];
            cell = cell_pay;
        }
            break;
            
        default:
            cell = [BaseTableViewCell cellWithTableView:tableView];
            cell.backgroundColor = kRandomColor;
            break;
    }
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    footView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(10);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
//    DLog(@"++++++++++++++++++=   %.2f",offsetY);
    if (self.delegate && [self.delegate respondsToSelector:@selector(scrollViewDidScroll_y:)]) {
        [self.delegate scrollViewDidScroll_y:offsetY];
    }
}

@end
